package oa.web.controller;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;

import oa.biz.AnnouncementBiz;
import oa.biz.EmployeeBiz;
import oa.entity.Announcement;
import oa.entity.Employee;

@Controller
@RequestMapping("/admin")
public class AnnouncementController {
	@Autowired
	private AnnouncementBiz paperBiz;
	@Autowired
	private EmployeeBiz employeeBiz;

	@RequestMapping("/getPaper")
	@ResponseBody
	public List<Announcement> getList(@RequestParam(name="departmentId",required=false,defaultValue="0")int departmentId){
		if(departmentId==0){
			return paperBiz.getAll();
		}else{
			return paperBiz.departmentPaper(departmentId);
		}
		
	}
	
	@RequestMapping("/paperList")
	public String paperList(){
		return "paperList";
	}
	
	@RequestMapping("/paperDetail")
	@ResponseBody
	public Announcement paperDetail(int id){
		Announcement target =paperBiz.fetchById(id);
		List<Employee> employee=employeeBiz.getAll();
		for(Employee e: employee){
			if(e.getId()==target.getApproverId()){
				target.setApprover(e.getName());
			};
		}
		return target;
	}
	@RequestMapping("/paperManage")
	public String paperManage(Model model,int creatorId){
		model.addAttribute("announcements", paperBiz.fetchByUser(creatorId));
		return "paperManage";
		
	}

	@RequestMapping("/newAnnouncement")
	public String newpaper(){
		return "Announcement";
	}
	
	@RequestMapping("/AnnouncementSave")
	public String saveAnnouncement(@RequestParam(name="id",defaultValue="0",required=false)Integer id,Announcement announcement){
		if(id==0){
			announcement.setCreateTime(new Date(System.currentTimeMillis()));	
			announcement.setLastEditTime(new Date(System.currentTimeMillis()));
			paperBiz.addAnnouncement(announcement);
			return "redirect:paperManage?creatorId="+announcement.getCreatorId()+"";		
		}else{
			announcement.setLastEditTime(new Date(System.currentTimeMillis()));
			paperBiz.updateAnnoucement(announcement);
			return "redirect:paperManage?creatorId="+announcement.getCreatorId()+"";
		}
		}
	@RequestMapping("/editAnnouncement")
	public String editAnnouncement(Model model,int id){
		model.addAttribute("paper",paperBiz.fetchById(id));
		return "Announcement";	
	}
	@RequestMapping("/paperApprove")
		public String paperApprove(Model model){
			model.addAttribute("announcements",paperBiz.fetchByStatus(2));
			return "paperApprove";
			
		}
	@RequestMapping("/approvePaper")
	public String approvePaper(int id,int approverId){
		Announcement paper=paperBiz.fetchById(id);
		paper.setStatus(3);
		paper.setApproveTime(new Date(System.currentTimeMillis()));
		paper.setApproverId(approverId);
		paperBiz.updateAnnoucement(paper);
		return "redirect:paperApprove";
	}
	@RequestMapping("/unapprovePaper")
	public String unapprovePaper(int id){
		Announcement paper=paperBiz.fetchById(id);
		paper.setStatus(1);
		paperBiz.updateAnnoucement(paper);
		return "redirect:paperApprove";
	}
	@RequestMapping("/paperDetailApprove")
	public String paperDetailApprove(int id,Model model){
		model.addAttribute("paper",paperBiz.fetchById(id));
		model.addAttribute("Approving",1);
		return "paperDetailApprove";
		
	}
	@RequestMapping("/paperOne")
	public String paperOne(int id,Model model){
		Announcement target =paperBiz.fetchById(id);
		
		List<Employee> employee=employeeBiz.getAll();
		for(Employee e: employee){
			if(e.getId()==target.getApproverId()){
				target.setApprover(e.getName());
			}
		}
		model.addAttribute("Announcement",target);
		return "paperList";
	}
}

